<template>
  <div class="list">
    <!-- 搜索框 -->
    <div style="margin-top:15px;">
      <el-input
        placeholder="请输入内容"
        v-model="query"
        class="input-with-select"
        style="width:300px;"
      >
        <!-- 点击实现添加用户效果 -->
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="searchUser"
        ></el-button>
      </el-input>
      <el-button
        type="success"
        plain
        @click="$router.push({name:'Add'})"
      >添加用户</el-button>
    </div>
    <!-- 列表 -->
    <template>
      <el-table
        :data="goodsList"
        border
        style="width: 100%;margin-top:15px"
      >
        <el-table-column
          type="index"
          width="50"
        > </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
        > </el-table-column>
        <el-table-column
          prop="goods_price"
          label="价格"
          width="100"
        > </el-table-column>
        <el-table-column
          prop="goods_weight"
          label="重量"
          width="100"
        > </el-table-column>
        <el-table-column
          prop="upd_time"
          label="创建时间"
          width="200"
        > </el-table-column>
        <el-table-column
          label="操作"
          width="200"
        >
          <!-- 插槽 -->
          <template slot-scope="scope">
            <el-tooltip
              content="编辑"
              placement="top"
            >
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                plain
                @click="handleEdit(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              content="删除"
              placement="top"
            >
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                plain
                @click="handleDelete(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 分页 -->
    <el-pagination
      style="margin-top:15px;background-color:#ccc;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total" >
    </el-pagination>

    <!-- 编辑弹框 -->
     <el-dialog title="编辑用户信息" :visible.sync="editdialogFormVisible">
      <el-form :model="editform" :rules="rules" ref="editform" label-width="100px">
        <el-form-item label="商品名称" prop='goods_name'>
          <el-input v-model="editform.goods_name" auto-complete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品价格" label-width="100px" prop='goods_price'>
          <el-input v-model="editform.goods_price" auto-complete="off" type='number'></el-input>
        </el-form-item>
        <el-form-item label="商品数量" label-width="100px"  prop='goods_number'>
          <el-input v-model="editform.goods_number" auto-complete="off" type='number'></el-input>
        </el-form-item>
        <el-form-item label="商品重量" label-width="100px"  prop='goods_weight'>
          <el-input v-model="editform.goods_weight" auto-complete="off" type='number'></el-input>
        </el-form-item>
        <el-form-item label="创建时间" label-width="100px"  prop='add_time'>
          <el-input v-model="editform.add_time" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品分类">
          <!-- options 可选项数据源 -->
          <!--  @change="handleChange" -->
          <el-cascader
            :options="cateList"
            v-model="selectedOptions"
            :props='goodprops'
            :clearable='true' >
          </el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editdialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodds('editform')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getGoodsList, getCategoriesList, putGoodsInfo } from '@/api'
export default {
  data () {
    return {
      goodsList: [],
      query: '',
      pagenum: 1,
      pagesize: 10,
      total: 0,
      // 控制编辑弹框的显示或隐藏
      editdialogFormVisible: false,
      // 编辑商品信息
      editform: {
        goods_id: '',
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
        add_time: ''
      },
      // 商品分类
      cateList: [],
      selectedOptions: [],
      goodprops: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 验证规则
      rules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ],
        add_time: [
          { required: true, message: '请输入创建时间', trigger: 'blur,change' }
        ]
      }
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    // 动态渲染数据
    init () {
      getGoodsList({
        query: this.query,
        pagenum: this.pagenum,
        pagesize: this.pagesize
      }).then(res => {
        // console.log(res)
        if (res.meta.status === 200) {
          this.goodsList = res.data.goods
          this.total = res.data.total
        }
      })
    },
    // 关键字搜索
    searchUser () {
      getGoodsList({ query: this.query, pagenum: 1, pagesize: 10 }).then(
        res => {
          // console.log(res)
          if (res.meta.status === 200) {
            this.goodsList = res.data.goods
            this.total = res.data.total
          }
        }
      )
    },
    // 实现编辑商品信息功能
    handleEdit (row) {
      console.log(row)
      // 显示弹框
      this.editdialogFormVisible = true
      // 显示商品原值
      this.editform.goods_name = row.goods_name
      this.editform.goods_price = row.goods_price
      this.editform.goods_number = row.goods_number
      this.editform.goods_weight = row.goods_weight
      this.editform.add_time = row.add_time
      this.editform.goods_id = row.goods_id
      // 展示商品分类信息
      getCategoriesList(3).then(res => {
      // console.log(res)
        if (res.meta.status === 200) {
          this.cateList = res.data
          console.log(this.cateList)
        }
      })
    },
    handleDelete (row) {},
    // 分页
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.init()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.pagenum = val
      this.init()
    },
    // 商品编辑提交
    editGoodds (formInfo) {
      this.$refs[formInfo].validate(valid => {
        if (valid) {
          console.log(this.goodprops)

          // 调用接口，实现编辑功能的提交
          putGoodsInfo(this.editform)
            .then(res => {
              console.log(res)
            })
        } else {
          this.$message.error('数据不完整')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
